import 'package:flutter/material.dart';
import 'package:loading_indicator/loading_indicator.dart';
import 'package:flutternettest/global/application.dart';
import 'package:flutternettest/global/routers.dart';
import 'package:flutternettest/model/login_info_entity.dart';
import 'package:flutternettest/model/theme_entity.dart';
import 'package:flutternettest/utils/data_utils.dart';
import 'package:flutternettest/utils/utils.dart';

class RegistPage extends StatefulWidget {
  @override
  _RegistPageState createState() => _RegistPageState();
}

class _RegistPageState extends State<RegistPage> {
  GlobalKey<FormState> _registFormKey = GlobalKey();

  GlobalDimen mDimen = Application.mDimens;
  GlobalColor mColors = Application.mColors;

  String userName = '';
  String passWord = '';
  String passWordRepeat = '';
  bool showPassword = false;
  bool _loading = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("注册账号")),
      body: Container(
          color: Color(Application.mColors.backgroundColor),
          child: Column(
            children: <Widget>[
              buildLoginForm(context),
              buildRegistBtn(context)
            ],
          )),
    );
  }

  ///用户名和密码框
  Container buildLoginForm(BuildContext context) {
    return Container(
      width: MediaQuery
          .of(context)
          .size
          .width,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
            bottomLeft: Radius.circular(8.0),
            bottomRight: Radius.circular(8.0)),
        color: Color(mColors.transBgWhite),
      ),
      child: Column(
        children: <Widget>[
          Form(
            key: _registFormKey,
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(left: 15, right: 15),
                  child: TextFormField(
                    decoration: InputDecoration(
                      hintText: '用户名',
                      border: InputBorder.none,
                      prefixIcon: Icon(
                        Icons.person_outline,
                        color: Color(mColors.primaryColor),
                      ),
                    ),
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return "用户名不能为空";
                      } else {
                        return null;
                      }
                    },
                    onSaved: (value) {
                      setState(() {
                        userName = value;
                      });
                    },
                  ),
                ),
                Container(
                  color: Colors.grey,
                  height: 1,
                  margin: EdgeInsets.only(left: 15, right: 15),
                ),
                Padding(
                    padding: EdgeInsets.only(left: 15, right: 15),
                    child: TextFormField(
                      obscureText: !showPassword,
                      decoration: InputDecoration(
                          hintText: '密码',
                          prefixIcon: Icon(
                            Icons.lock_outline,
                            color: Color(mColors.primaryColor),
                          ),
                          border: InputBorder.none,
                          suffixIcon: IconButton(
                            icon: Icon(
                              Icons.remove_red_eye,
                              color: showPassword
                                  ? Color(mColors.primaryColor)
                                  : Color(mColors.disableColor),
                            ),
                            onPressed: _showPassword,
                          )),
                      validator: (value) {
                        if (value == null || value.isEmpty) {
                          return "密码不能为空";
                        } else {
                          return null;
                        }
                      },
                      onSaved: (value) {
                        setState(() {
                          passWord = value;
                        });
                      },
                    )),
                Container(
                  color: Colors.grey,
                  height: 1,
                  margin: EdgeInsets.only(left: 15, right: 15),
                ),
                Padding(
                    padding: EdgeInsets.only(left: 15, right: 15),
                    child: TextFormField(
                      obscureText: !showPassword,
                      decoration: InputDecoration(
                        hintText: '重复密码',
                        prefixIcon: Icon(
                          Icons.lock,
                          color: Color(mColors.primaryColor),
                        ),
                        border: InputBorder.none,
                      ),
                      validator: (value) {
                        if (passWord.isEmpty) {
                          _registFormKey.currentState.save();
                        }
                        if (value == null || value.isEmpty ||value != passWord) {
                          return "两次密码不一致";
                        } else {
                          return null;
                        }
                      },
                      onSaved: (value) {
                        setState(() {
                          passWordRepeat = value;
                        });
                      },
                    )),
              ],
            ),
          )
        ],
      ),
    );
  }

  ///登录按钮
  Container buildRegistBtn(BuildContext context) {
    return Container(
        child: GestureDetector(
          child: Container(
              margin: EdgeInsets.only(top: 30.0),
              decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: _loading
                      ? Color(mColors.textColorWithPrimary)
                      : Color(mColors.primaryColor),
                  border: Border.all(color: Colors.white, width: 1.0)),
              child: Center(
                child: Padding(
                  padding: EdgeInsets.all(12.0),
                  child: _loading
                      ? Container(
                      width: 29,
                      height: 29,
                      child: Center(
                          child: LoadingIndicator(
                              indicatorType: Indicator.lineSpinFadeLoader,
                              color: Color(mColors.primaryColor)),
                      )
                  )
                      : Container(
                      width: 29,
                      height: 29,
                      child: Center(
                        child: Text(
                          '注册',
                          style: TextStyle(
                              color: Colors.white, fontSize: mDimen.commonSize),
                        ),
                      )),
                ),
              )),
          onTap: doRegist,
        ));
  }

  void _showPassword() {
    setState(() {
      showPassword = !showPassword;
    });
  }

  void doRegist() {
    if (_registFormKey.currentState.validate()) {
      setState(() {
        _loading = true;
      });
      _registFormKey.currentState.save();
      DataUtil.doRegist({
        'username': userName,
        'password': passWord,
        'repassword': passWordRepeat
      }).then((res) {
        setState(() {
          _loading = false;
        });
        if (res != null) {
          Utils.showToast("注册成功，请登录");
          Navigator.of(context).pop();
        }else{
          Navigator.of(context).pushNamed(Routers.loginPagePath,arguments: userName);
        }
      });
    }
  }
}
